<HTML> 
<HEAD> 
	<TITLE>3rd Party ROBOTC Drivers</TITLE> 
<link href="tabs.css" rel="stylesheet" type="text/css"/> 
<link href="doxygen.css" rel="stylesheet" type="text/css"/> 
</HEAD> 
 
<BODY bgcolor="#ffffff" link="#000000" vlink="#000000"> 
<table width="100%" bgcolor="navy" cellspacing=0 cellpadding=1 border=0> 
<tr><td><table width="100%" bgcolor="#EEEEDD" cellspacing=0 cellpadding=3 border=0> 
<tr> 
    <td width="33%" align="left"> <img src="images/LOGO_NXT.gif" width=266 height=44 border="0" alt="Mindstorms"></td> 
    <td><b><font size="+3" color="navy">3rd Party ROBOTC Drivers</font></b></td> 
    <td align="right"> <img src="images/logo.png" width=44 height=44 border="0" alt="RobotC"></td>     
</tr> 
  <tr bgcolor="#cccc99" align="right"> 
          <td colspan=3> <font face="arial" size="-1"> [<a href=main.html>Home</a>] [<a target=_top href="https://sourceforge.net/projects/rdpartyrobotcdr/">Download</a>] [<a target=_top href="http://apps.sourceforge.net/mantisbt/rdpartyrobotcdr/my_view_page.php">Submit a bug/suggestion</a>]  [<a target=_top href="http://www.robotc.net/forums/">ROBOTC Forums</a>] [<a target=_top href="http://botbench.com">Blog</a>] [<a target=_top href="http://sourceforge.net/donate/index.php?group_id=257238">Support this project</a>]</font> </td> 
</tr> 
</table></td></tr> 
</table> 
<!-- Generated by Doxygen 1.7.2 -->
<div class="header">
  <div class="headertitle">
<h1>benedettelli-nxt2wifi-test1.c</h1>  </div>
</div>
<div class="contents">
<div class="fragment"><pre class="fragment"><span class="preprocessor">#pragma config(Sensor, S1,     HTBM,           sensorI2CCustom)</span>
<span class="preprocessor"></span><span class="preprocessor">#pragma config(Sensor, S2,     LIGHT,          sensorLightInactive)</span>
<span class="preprocessor"></span><span class="preprocessor">#pragma config(Sensor, S3,     SOUND,          sensorSoundDB)</span>
<span class="preprocessor"></span><span class="preprocessor">#pragma config(Sensor, S4,     NXT2WIFI,       sensorHighSpeed)</span>
<span class="preprocessor"></span><span class="comment">//*!!Code automatically generated by &#39;ROBOTC&#39; configuration wizard               !!*//</span>

<span class="comment">/*</span>
<span class="comment"> * $Id: benedettelli-nxt2wifi-test1.c 133 2013-03-10 15:15:38Z xander $</span>
<span class="comment"> */</span>
<span class="comment"></span>
<span class="comment">/**</span>
<span class="comment"> * benedettelli-nxt2wifi.h provides an API for Daniele&#39;s NXT2WiFi Sensor. This program</span>
<span class="comment"> * demonstrates how to use that API.  This program is an SNMP agent implementation.</span>
<span class="comment"> * Query it with:</span>
<span class="comment"> *</span>
<span class="comment"> * Fetch current temperature: snmpget  -r 0 -c public -v 1 $IP iso.3.6.1.3.1</span>
<span class="comment"> * Fetch current pressure: snmpget  -r 0 -c public -v 1 $IP iso.3.6.1.3.2</span>
<span class="comment"> * Fetch current sound level: snmpget  -r 0 -c public -v 1 $IP iso.3.6.1.3.3</span>
<span class="comment"> * Fetch current ambient light level: snmpget  -r 0 -c public -v 1 $IP iso.3.6.1.3.4</span>
<span class="comment"> * To query the brick&#39;s friendly name, use snmpget  -r 0 -c public -v 1 $IP iso.3.6.1.2.1.1.5.0</span>
<span class="comment"> *</span>
<span class="comment"> * More info here: http://botbench.com/blog/2012/06/11/little-big-data-splunk-meets-nxt/</span>
<span class="comment"> *</span>
<span class="comment"> * Changelog:</span>
<span class="comment"> * - 0.1: Initial release</span>
<span class="comment"> *</span>
<span class="comment"> * Credits:</span>
<span class="comment"> * - Big thanks to Daniele Benedettelli for providing me with the hardware necessary to write and test this.</span>
<span class="comment"> *</span>
<span class="comment"> * License: You may use this code as you wish, provided you give credit where it&#39;s due.</span>
<span class="comment"> *</span>
<span class="comment"> * THIS CODE WILL ONLY WORK WITH ROBOTC VERSION 3.59 AND HIGHER. </span>
<span class="comment"></span>
<span class="comment"> * Xander Soldaat (xander_at_botbench.com)</span>
<span class="comment"> * 22 July 2012</span>
<span class="comment"> * version 0.1</span>
<span class="comment"> */</span>

<span class="preprocessor">#include &quot;<a class="code" href="common_8h.html" title="Commonly used functions used by drivers.">drivers/common.h</a>&quot;</span>
<span class="preprocessor">#include &quot;<a class="code" href="timer_8h.html" title="Additional _timers for ROBOTC.">drivers/timer.h</a>&quot;</span>
<span class="preprocessor">#include &quot;<a class="code" href="hitechnic-barometer_8h.html" title="HiTechnic Barometric Sensor driver.">drivers/hitechnic-barometer.h</a>&quot;</span>
<span class="preprocessor">#include &quot;<a class="code" href="benedettelli-nxt2wifi_8h.html" title="Dani&amp;#39;s WiFi Sensor.">drivers/benedettelli-nxt2wifi.h</a>&quot;</span>

<span class="comment">// These are the ASN constants as used by SNMP</span>
<span class="preprocessor">#define ASN_INTEGER           0x02</span>
<span class="preprocessor"></span><span class="preprocessor">#define ASN_OCTET_STRING      0x04</span>
<span class="preprocessor"></span><span class="preprocessor">#define ASN_NULL              0x05</span>
<span class="preprocessor"></span><span class="preprocessor">#define ASN_OBJECT_IDENTIFIER 0x06</span>
<span class="preprocessor"></span><span class="preprocessor">#define ASN_SEQUENCE          0x30</span>
<span class="preprocessor"></span><span class="preprocessor">#define ASN_IPADDRESS         0x40</span>
<span class="preprocessor"></span><span class="preprocessor">#define ASN_COUNTER32         0x41</span>
<span class="preprocessor"></span><span class="preprocessor">#define ASN_GAUGE32           0x42</span>
<span class="preprocessor"></span><span class="preprocessor">#define ASN_TIMETICKS         0x43</span>
<span class="preprocessor"></span>
<span class="comment">// These are SNMP protocol specific constants</span>
<span class="preprocessor">#define SNMP_VERSION_1        0x00</span>
<span class="preprocessor"></span><span class="preprocessor">#define SNMP_GET_REQUEST      0xA0</span>
<span class="preprocessor"></span><span class="preprocessor">#define SNMP_NEXT_REQUEST     0xA1</span>
<span class="preprocessor"></span><span class="preprocessor">#define SNMP_GET_RESPONSE     0xA2</span>
<span class="preprocessor"></span><span class="preprocessor">#define SNMP_SET_REQUEST      0x03</span>
<span class="preprocessor"></span>
<span class="keywordtype">long</span> reqID;

<span class="keywordtype">long</span> dataTemp = 0;
<span class="keywordtype">long</span> dataPress = 0;
<span class="keywordtype">long</span> dataLight = 0;
<span class="keywordtype">long</span> dataSound = 0;
<span class="keywordtype">long</span> txbytes = 0;
<span class="keywordtype">long</span> rxbytes = 0;
<span class="keywordtype">string</span> IPaddress = <span class="stringliteral">&quot;0.0.0.0&quot;</span>;
<span class="keywordtype">string</span> connStatus = <span class="stringliteral">&quot;disconnected&quot;</span>;
<a name="a0"></a><a class="code" href="group__common.html#gacc2928041c5ff8e7f2ddef1e88431e80">tHugeByteArray</a> data;
<a class="code" href="group__common.html#gacc2928041c5ff8e7f2ddef1e88431e80">tHugeByteArray</a> payloadData;

task updateScreen()
{
  <span class="keywordflow">while</span>(<span class="keyword">true</span>)
  {
    nxtDisplayTextLine(0, <span class="stringliteral">&quot;Stat: %s&quot;</span>, connStatus);
    nxtDisplayTextLine(1, <span class="stringliteral">&quot;%s&quot;</span>,IPaddress);
    nxtDisplayTextLine(2, <span class="stringliteral">&quot;-------------------&quot;</span>);
    nxtDisplayTextLine(3, <span class="stringliteral">&quot;Temp : %2.2f&quot;</span>, (<span class="keywordtype">float</span>)dataTemp/100.0);
    nxtDisplayTextLine(4, <span class="stringliteral">&quot;Press: %d&quot;</span>, dataPress);
    nxtDisplayTextLine(5, <span class="stringliteral">&quot;Light: %d&quot;</span>, dataLight);
    nxtDisplayTextLine(6, <span class="stringliteral">&quot;Sound: %d&quot;</span>, dataSound);
    nxtDisplayTextLine(7, <span class="stringliteral">&quot;RX/TX: %d/%d&quot;</span>, rxbytes, txbytes);
    wait1Msec(100);
  }
}

<span class="comment">// Constantly read the sensors</span>
task pollSensors()
{
  <span class="keywordflow">while</span> (<span class="keyword">true</span>)
  {
    dataPress = <a name="a1"></a><a class="code" href="group___h_t_b_m.html#ga8437567f6fabf1dea365809ae1b68c1d">HTBMreadMInHg</a>(HTBM);
    wait1Msec(100);
    dataTemp = <a name="a2"></a><a class="code" href="group___h_t_b_m.html#gabdb40711a881155269d86ef6015cee65">HTBMreadTemp</a>(HTBM) * 100;
    wait1Msec(100);
    dataLight = SensorValue[LIGHT];
    wait1Msec(100);
    dataSound = SensorValue[SOUND];
    wait1Msec(100);
  }
}

<span class="keywordtype">int</span> genSNMPreply(<span class="keywordtype">long</span> reqNo, <span class="keywordtype">string</span> &amp;communityName, <a name="a3"></a><a class="code" href="group__common.html#gaa172ef131853460e5b9f5c6db81d30f4">tByteArray</a> &amp;oid, ubyte oidlen, ubyte dataType, <a class="code" href="group__common.html#gacc2928041c5ff8e7f2ddef1e88431e80">tHugeByteArray</a> &amp;payloadData, <span class="keywordtype">int</span> size)
{
  ubyte communityNameLen = strlen(communityName);
  ubyte payloadSize = size;

  <span class="comment">// Precalculate all the offsets</span>
  ubyte OFFS_VERS = 2;
  ubyte OFFS_COMM = 5;
  ubyte OFFS_RQTP = 7 + strlen(communityName);
  ubyte OFFS_RQID = 2 + OFFS_RQTP;
  ubyte OFFS_ERNO = 6 + OFFS_RQID;
  ubyte OFFS_ERID = 3 + OFFS_ERNO;
  ubyte OFFS_SEQ1 = 3 + OFFS_ERID;
  ubyte OFFS_SEQ2 = 2 + OFFS_SEQ1;
  ubyte OFFS_OBID = 2 + OFFS_SEQ2;
  ubyte OFFS_DATA = oidlen + 2 + OFFS_OBID;
  ubyte TOTL_SIZE = 2 + OFFS_DATA + payloadSize;


  memset(data, 0, <span class="keyword">sizeof</span>(data));

  writeDebugStreamLine(<span class="stringliteral">&quot;TOTL_SIZE: %d&quot;</span>, TOTL_SIZE);
  writeDebugStreamLine(<span class="stringliteral">&quot;oidlen: %d&quot;</span>, oidlen);
  writeDebugStreamLine(communityName);

  <span class="comment">// Inital sequence marker</span>
  data[0] = 0x30;
  data[1] = OFFS_DATA + payloadSize;

  <span class="comment">// SNMP version part</span>
  data[OFFS_VERS + 0] = ASN_INTEGER;
  data[OFFS_VERS + 1] = 1;
  data[OFFS_VERS + 2] = SNMP_VERSION_1;

  <span class="comment">// Community</span>
  data[OFFS_COMM + 0] = ASN_OCTET_STRING;
  data[OFFS_COMM + 1] = communityNameLen;
  memcpy(&amp;data[OFFS_COMM + 2], communityName, communityNameLen);

  <span class="comment">// Request type</span>
  data[OFFS_RQTP + 0] = SNMP_GET_RESPONSE;
  data[OFFS_RQTP + 1] = TOTL_SIZE - OFFS_RQID;

  <span class="comment">// Request ID</span>
  <span class="comment">// 6=requestID, 3=error, 3=erroridx, 2=sequence, 2=sequence,2=payload</span>
  data[OFFS_RQID + 0] = ASN_INTEGER;
  data[OFFS_RQID + 1] = 4;  <span class="comment">// we&#39;re sending a long</span>
  data[OFFS_RQID + 2] = (reqNo &gt;&gt; 24) &amp; 0xFF;
  data[OFFS_RQID + 3] = (reqNo &gt;&gt; 16) &amp; 0xFF;
  data[OFFS_RQID + 4] = (reqNo &gt;&gt;  8) &amp; 0xFF;
  data[OFFS_RQID + 5] = (reqNo &gt;&gt;  0) &amp; 0xFF;

  <span class="comment">// error</span>
  data[OFFS_ERNO + 0] = ASN_INTEGER;
  data[OFFS_ERNO + 1] = 1;
  data[OFFS_ERNO + 2] = 0;

  <span class="comment">// erorr index</span>
  data[OFFS_ERID + 0] = ASN_INTEGER;
  data[OFFS_ERID + 1] = 1;
  data[OFFS_ERID + 2] = 0;

  <span class="comment">// sequence 1</span>
  data[OFFS_SEQ1 + 0] = ASN_SEQUENCE;
  data[OFFS_SEQ1 + 1] = TOTL_SIZE - OFFS_SEQ2;

  <span class="comment">// sequence 2</span>
  data[OFFS_SEQ2 + 0] = ASN_SEQUENCE;
  data[OFFS_SEQ2 + 1] = TOTL_SIZE - OFFS_OBID;

  <span class="comment">// OID</span>
  data[OFFS_OBID + 0] = ASN_OBJECT_IDENTIFIER;
  data[OFFS_OBID + 1] = oidlen;
  memcpy(&amp;data[OFFS_OBID + 2], oid, oidlen);

  data[OFFS_DATA + 0] = dataType;
  data[OFFS_DATA + 1] = payloadSize;
  memcpy(&amp;data[OFFS_DATA + 2], payloadData, payloadSize);

  <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> i = 0; i &lt; TOTL_SIZE;i++)
  {
    <span class="keywordflow">if</span> ((i % 8) == 0)
      writeDebugStreamLine(<span class="stringliteral">&quot;&quot;</span>);

    writeDebugStream(<span class="stringliteral">&quot;0x%02x &quot;</span>, data[i] &amp; 0xFF);
  }
  <span class="keywordflow">return</span> TOTL_SIZE;
}


<span class="keywordtype">int</span> genSNMPreply(<span class="keywordtype">long</span> reqNo, <span class="keywordtype">string</span> &amp;communityName, <a class="code" href="group__common.html#gaa172ef131853460e5b9f5c6db81d30f4">tByteArray</a> &amp;oid, ubyte oidlen, <span class="keywordtype">string</span> &amp;dataString)
{
  <span class="keywordtype">int</span> len = strlen(dataString);
  memcpy(payloadData, dataString, len);
  <span class="keywordflow">return</span> genSNMPreply(reqNo, communityName, oid, oidlen, (ubyte)ASN_OCTET_STRING, payloadData, len);
}

<span class="keywordtype">int</span> genSNMPreply(<span class="keywordtype">long</span> reqNo, <span class="keywordtype">string</span> &amp;communityName, <a class="code" href="group__common.html#gaa172ef131853460e5b9f5c6db81d30f4">tByteArray</a> &amp;oid, ubyte oidlen, <span class="keywordtype">long</span> value)
{
  payloadData[0] = (value &gt;&gt; 24) &amp; 0xFF;
  payloadData[1] = (value &gt;&gt; 16) &amp; 0xFF;
  payloadData[2] = (value &gt;&gt;  8) &amp; 0xFF;
  payloadData[3] = (value &gt;&gt;  0) &amp; 0xFF;
  <span class="comment">//memcpy(payloadData, value, len);</span>
  <span class="keywordflow">return</span> genSNMPreply(reqNo, communityName, oid, oidlen, (ubyte)ASN_INTEGER, payloadData, 4);
}

<span class="keywordtype">bool</span> SNMPdecode (<span class="keywordtype">long</span> &amp;reqID, <a class="code" href="group__common.html#gaa172ef131853460e5b9f5c6db81d30f4">tByteArray</a> &amp;oid)
{
  ubyte length = <a name="a4"></a><a class="code" href="group__common-rs485.html#gae1645d814439623db869c5bfb5d4bd32">RS485rxbuffer</a>[1];
  ubyte snmpversion = <a class="code" href="group__common-rs485.html#gae1645d814439623db869c5bfb5d4bd32">RS485rxbuffer</a>[4] + 1;
  <span class="keywordtype">string</span> community;
  ubyte communityLength = <a class="code" href="group__common-rs485.html#gae1645d814439623db869c5bfb5d4bd32">RS485rxbuffer</a>[6];
  memcpy(community, &amp;<a class="code" href="group__common-rs485.html#gae1645d814439623db869c5bfb5d4bd32">RS485rxbuffer</a>[7], communityLength);

  ubyte reqTypeOffset = 7 + communityLength;
  ubyte reqType = <a class="code" href="group__common-rs485.html#gae1645d814439623db869c5bfb5d4bd32">RS485rxbuffer</a>[reqTypeOffset];

  ubyte reqIDOffset = reqTypeOffset + 4;
  reqID = ((long)(<a class="code" href="group__common-rs485.html#gae1645d814439623db869c5bfb5d4bd32">RS485rxbuffer</a>[reqIDOffset + 0] &amp; 0xFF) &lt;&lt; 24) +
               ((<span class="keywordtype">long</span>)(<a class="code" href="group__common-rs485.html#gae1645d814439623db869c5bfb5d4bd32">RS485rxbuffer</a>[reqIDOffset + 1] &amp; 0xFF) &lt;&lt; 16) +
               ((long)(<a class="code" href="group__common-rs485.html#gae1645d814439623db869c5bfb5d4bd32">RS485rxbuffer</a>[reqIDOffset + 2] &amp; 0xFF) &lt;&lt;  8) +
               ((<span class="keywordtype">long</span>)(<a class="code" href="group__common-rs485.html#gae1645d814439623db869c5bfb5d4bd32">RS485rxbuffer</a>[reqIDOffset + 3] &amp; 0xFF) &lt;&lt;  0);

  ubyte error = <a class="code" href="group__common-rs485.html#gae1645d814439623db869c5bfb5d4bd32">RS485rxbuffer</a>[reqIDOffset + 6];
  ubyte errorIdx = <a class="code" href="group__common-rs485.html#gae1645d814439623db869c5bfb5d4bd32">RS485rxbuffer</a>[reqIDOffset + 9];

  ubyte oidOffset = reqIDOffset + 15;
  ubyte oidSize = <a class="code" href="group__common-rs485.html#gae1645d814439623db869c5bfb5d4bd32">RS485rxbuffer</a>[oidOffset];
  <span class="comment">//ubyte oid[9];</span>
  memcpy(oid, &amp;<a class="code" href="group__common-rs485.html#gae1645d814439623db869c5bfb5d4bd32">RS485rxbuffer</a>[oidOffset + 1], oidSize);
  ubyte valueOffset = oidOffset + oidSize;
  ubyte valueType = <a class="code" href="group__common-rs485.html#gae1645d814439623db869c5bfb5d4bd32">RS485rxbuffer</a>[valueOffset];
  ubyte valueSize = <a class="code" href="group__common-rs485.html#gae1645d814439623db869c5bfb5d4bd32">RS485rxbuffer</a>[valueOffset + 1];
  <span class="keywordtype">string</span> valueString;

  <span class="keywordflow">switch</span>(valueType)
  {
    <span class="keywordflow">case</span> ASN_INTEGER: <span class="keywordflow">break</span>;
    <span class="keywordflow">case</span> ASN_OCTET_STRING:
      memcpy(valueString, &amp;<a class="code" href="group__common-rs485.html#gae1645d814439623db869c5bfb5d4bd32">RS485rxbuffer</a>[valueOffset + 2], valueSize);
      <span class="keywordflow">break</span>;
    <span class="keywordflow">case</span> ASN_NULL: <span class="keywordflow">break</span>;
  }

  writeDebugStreamLine(<span class="stringliteral">&quot;Length: %d&quot;</span>, length);
  writeDebugStreamLine(<span class="stringliteral">&quot;SNMP version: %d&quot;</span>, snmpversion);
  writeDebugStreamLine(<span class="stringliteral">&quot;community: %s&quot;</span>, community);
  writeDebugStreamLine(<span class="stringliteral">&quot;Req Type: 0x%X&quot;</span>, reqType);
  writeDebugStreamLine(<span class="stringliteral">&quot;Reqid: 0x%8X (%d)&quot;</span>, reqID, reqID);
        writeDebugStreamLine(<span class="stringliteral">&quot;error: 0x%X&quot;</span>, error);
        writeDebugStreamLine(<span class="stringliteral">&quot;errorIdx: 0x%X&quot;</span>, errorIdx);
        writeDebugStreamLine(<span class="stringliteral">&quot;oidSize: %d&quot;</span>, oidSize);
        <span class="keywordflow">if</span> (oidSize &gt; 9)
          <span class="keywordflow">return</span> <span class="keyword">false</span>;
        writeDebugStream(<span class="stringliteral">&quot;OID: &quot;</span>);
        <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; (oidSize - 1); i++)
        {
          writeDebugStream(<span class="stringliteral">&quot;%d.&quot;</span>, oid[i]);
        }
        writeDebugStreamLine(<span class="stringliteral">&quot;%d&quot;</span>, oid[oidSize - 1]);
        writeDebugStreamLine(<span class="stringliteral">&quot;valueType: 0x%X&quot;</span>, valueType);
        writeDebugStreamLine(<span class="stringliteral">&quot;valueSize: 0x%X&quot;</span>, valueSize);
        <span class="keywordflow">if</span> (valueType == ASN_OCTET_STRING)
          writeDebugStreamLine(<span class="stringliteral">&quot;S: %s&quot;</span>, valueString);

        <span class="keywordflow">return</span> <span class="keyword">true</span>;
}


task main ()
{
  StartTask(pollSensors);
  StartTask(updateScreen);
  <span class="keywordtype">string</span> communityName = <span class="stringliteral">&quot;public&quot;</span>;
  <span class="keywordtype">string</span> dataString;
  getFriendlyName(dataString);

  <span class="keyword">static</span> <a class="code" href="group__common.html#gaa172ef131853460e5b9f5c6db81d30f4">tByteArray</a> oid;
  <span class="keyword">const</span> byte oidName[] = {43, 6, 1, 2, 1, 1, 5, 0};
  <span class="keyword">const</span> byte oidTree[] = {43, 6, 1, 3};

  <span class="keyword">const</span> ubyte oidTemp = 1;
  <span class="keyword">const</span> ubyte oidPress = 2;
  <span class="keyword">const</span> ubyte oidSound = 3;
  <span class="keyword">const</span> ubyte oidLight = 4;

  <span class="keywordtype">long</span> sensorData;

  <span class="keywordtype">int</span> avail = 0;
  <span class="keywordtype">int</span> totsize = 0;

  nNxtButtonTask = -2;

  <span class="comment">// initialise the port, etc</span>
  <a name="a5"></a><a class="code" href="group__common-rs485.html#ga681eadb60516210bbd261bc50d80dab7">RS485initLib</a>();

  memset(<a class="code" href="group__common-rs485.html#gae1645d814439623db869c5bfb5d4bd32">RS485rxbuffer</a>, 0, <span class="keyword">sizeof</span>(<a class="code" href="group__common-rs485.html#gae1645d814439623db869c5bfb5d4bd32">RS485rxbuffer</a>));
  memset(<a name="a6"></a><a class="code" href="group__common-rs485.html#ga76b40f6d5142bd650f910cd6cd19c588">RS485txbuffer</a>, 0, <span class="keyword">sizeof</span>(<a class="code" href="group__common-rs485.html#ga76b40f6d5142bd650f910cd6cd19c588">RS485txbuffer</a>));

  <span class="comment">// Disconnect if already connected</span>
  <a name="a7"></a><a class="code" href="group___n_x_t2_w_i_f_i.html#ga8ec5d0f6add0e5d953ad690f109a3253">N2WDisconnect</a>();
  <a name="a8"></a><a class="code" href="group___n_x_t2_w_i_f_i.html#ga8cad34ee07c9a294819f0070c94e1720">N2WchillOut</a>();

  <span class="comment">// if a custom profile exists, use that instead</span>
    <span class="keywordflow">if</span> (!<a name="a9"></a><a class="code" href="group___n_x_t2_w_i_f_i.html#ga518cfe843bc8f4d042405227a0a2f4ff">N2WCustomExist</a>())
  {
    StopTask(updateScreen);
    wait1Msec(50);
    eraseDisplay();
    PlaySound(soundException);
    nxtDisplayCenteredBigTextLine(1, <span class="stringliteral">&quot;ERROR&quot;</span>);
    nxtDisplayTextLine(3, <span class="stringliteral">&quot;No custom profile&quot;</span>);
    nxtDisplayTextLine(4, <span class="stringliteral">&quot;configured!!&quot;</span>);
    <span class="keywordflow">while</span>(<span class="keyword">true</span>) EndTimeSlice();
  }

  <a name="a10"></a><a class="code" href="group___n_x_t2_w_i_f_i.html#gac54a69b33b3a29473e8b4e80f993a401">N2WLoad</a>();

  wait1Msec(100);
  <a name="a11"></a><a class="code" href="group___n_x_t2_w_i_f_i.html#gaa0c803e53ea5cd1221aed19eea5463f4">N2WConnect</a>(<span class="keyword">true</span>);
  connStatus = <span class="stringliteral">&quot;connecting&quot;</span>;

  <span class="keywordflow">while</span> (!<a name="a12"></a><a class="code" href="group___n_x_t2_w_i_f_i.html#ga2012c5479768124f2083dc643d7b4573">N2WConnected</a>())
    wait1Msec(1000);

  connStatus = <span class="stringliteral">&quot;connected&quot;</span>;
  PlaySound(soundBeepBeep);

  wait1Msec(3000);
  <a name="a13"></a><a class="code" href="group___n_x_t2_w_i_f_i.html#ga75f932c01ab7aaf3ba965179b2773215">N2WgetIP</a>(IPaddress);

  wait1Msec(1000);
  <span class="keywordflow">if</span> (!<a name="a14"></a><a class="code" href="group___n_x_t2_w_i_f_i.html#gabf82b9ae3ba7b692ca6d9ec75eabc80e">N2WUDPOpenServer</a>(1, 161))
    writeDebugStreamLine(<span class="stringliteral">&quot;Err open port&quot;</span>);
  <span class="keywordflow">while</span> (<span class="keyword">true</span>)
  {
    avail = <a name="a15"></a><a class="code" href="group___n_x_t2_w_i_f_i.html#gabfed470eee5a127b1453966627552ca9">N2WUDPAvail</a>(1);
    <span class="keywordflow">if</span> (avail &gt; 0)
    {
      rxbytes += avail;
      <a class="code" href="group___n_x_t2_w_i_f_i.html#ga8cad34ee07c9a294819f0070c94e1720">N2WchillOut</a>();
      <a name="a16"></a><a class="code" href="group___n_x_t2_w_i_f_i.html#ga7a7ac805f528e56cc1de33f676670434">N2WUDPRead</a>(1, avail);
      <span class="keywordflow">if</span> (SNMPdecode(reqID, oid))
      {
                                writeDebugStreamLine(<span class="stringliteral">&quot;------------------&quot;</span>);
                                <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; <span class="keyword">sizeof</span>(oid); i++)
                                {
                                  writeDebugStream(<span class="stringliteral">&quot;0x%02x &quot;</span>, oid[i] &amp; 0xFF);
                                }
                                writeDebugStreamLine(<span class="stringliteral">&quot;&quot;</span>);
                                <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; <span class="keyword">sizeof</span>(oidTree); i++)
                                {
                                  writeDebugStream(<span class="stringliteral">&quot;0x%02x &quot;</span>, oidTree[i] &amp; 0xFF);
                                }
                                writeDebugStreamLine(<span class="stringliteral">&quot;\n------------------&quot;</span>);
              <span class="keywordflow">if</span> (memcmp(oid, oidName, 8) == 0)
              {
                getFriendlyName(dataString);
                      totsize = genSNMPreply(reqID, communityName, oid, 8, dataString);
                      <a name="a17"></a><a class="code" href="group___n_x_t2_w_i_f_i.html#ga7f7ff271d365cd3e9d37da277293869e">N2WUDPWrite</a>(1, data, totsize);
                      txbytes += totsize;
                    }
                    <span class="keywordflow">else</span> <span class="keywordflow">if</span> (memcmp(oid, oidTree, 4) == 0)
                    {
                      dataString = <span class="stringliteral">&quot;unknown&quot;</span>;

                      <span class="keywordflow">switch</span>(oid[4])
                      {
                        <span class="keywordflow">case</span> oidTemp:  sensorData = dataTemp; <span class="keywordflow">break</span>;
                        <span class="keywordflow">case</span> oidPress: sensorData = dataPress; <span class="keywordflow">break</span>;
                        <span class="keywordflow">case</span> oidSound: sensorData = dataSound; <span class="keywordflow">break</span>;
                        <span class="keywordflow">case</span> oidLight: sensorData = dataLight; <span class="keywordflow">break</span>;
                      }
                      totsize = genSNMPreply(reqID, communityName, oid, 5, sensorData);
                      <a class="code" href="group___n_x_t2_w_i_f_i.html#ga7f7ff271d365cd3e9d37da277293869e">N2WUDPWrite</a>(1, data, totsize);
                      txbytes += totsize;
                    }
                    <span class="keywordflow">else</span>
              {
                writeDebugStreamLine(<span class="stringliteral">&quot;unknown oid&quot;</span>);
              }
            }
            <span class="keywordflow">else</span>
            {
              writeDebugStreamLine(<span class="stringliteral">&quot;Invalid packet&quot;</span>);
            }
          }
    wait1Msec(100);
  }
}
</pre></div> </div>
</div>
<hr class="footer"/><address class="footer"><small>Generated on Wed Mar 13 2013 19:56:41 for ROBOTC Drivers by&#160;
<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.7.2 </small></address>
</body>
</html>
